﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using AutumnEngine.Modules;
using System.Diagnostics;
using AutumnEngine;

namespace AutumnEngine.Modules
{
    /// <summary>
    /// Controls touchscreen functions for the zune.
    /// </summary>
    public class TouchscreenInputModule : Module
    {
#if ZUNE
        TouchCollection last, current;
#endif
        public override void Create()
        {
#if ZUNE
            current = TouchPanel.GetState();
            last = current;
#endif
            base.Create();
        }

        public override void Update()
        {
#if ZUNE
            last = current;
            current = TouchPanel.GetState();
#endif
            base.Update();
        }

        /// <summary>
        /// Gets the current touches.
        /// </summary>
        /// <value>The current touches.</value>
        public List<Touch> CurrentTouches
        {
            get { return curCollectionToList(); }
        }

        /// <summary>
        /// Gets the last touches.
        /// </summary>
        /// <value>The last touches.</value>
        public List<Touch> LastTouches
        {
            get { return lastCollectionToList(); }
        }


        List<Touch> lastCollectionToList()
        {
            List<Touch> ret = new List<Touch>();
#if ZUNE

            foreach (TouchLocation l in last)
            {
                Touch rmp = new Touch();
                rmp.Positon = l.Position;
                rmp.Pressure = l.Pressure;
                rmp.Id = l.Id;
                ret.Add(rmp);
            }
#endif

            return ret;
        }

        List<Touch> curCollectionToList()
        {
            List<Touch> ret = new List<Touch>();
#if ZUNE

            foreach (TouchLocation l in current)
            {
                Touch rmp = new Touch();
                rmp.Positon = l.Position;
                rmp.Pressure = l.Pressure;
                rmp.Id = l.Id;
                ret.Add(rmp);
            }
#endif

            return ret;
        }

        /// <summary>
        /// Checks if the touch is registered.
        /// </summary>
        /// <param name="indx">The zero based index.</param>
        /// <returns></returns>
        public bool DoesTouchExist(int index)
        {
#if ZUNE
            if (this.current.Count >= index + 1)
                return true;
#endif
            return false;
        }

        /// <summary>
        /// Gets the total touches, starting at 0 for none and 1 for one.
        /// </summary>
        /// <value>The total touches.</value>
        public int TotalTouches
        {
            get 
            { 
#if ZUNE
                return this.current.Count;
#endif
                return 0;
            }
        }

    }

    public struct Touch
    {
        public Vector2 Positon;
        public float Pressure;
        public int Id;
    }
}
